import {Injectable} from '@angular/core';
import {HttpProvider} from "@providers/http";
import {Observable} from "rxjs/Observable";
import {env} from "@env/environment";

/*
  Generated class for the DataQueryServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DataQueryServiceProvider {
  constructor(public http: HttpProvider,) {
  }

  //获取微信支付的二维码
  getWXPayQrcode(data) {
    return this.http.get("/Inbound/invoke.do", {
      interfaceId: "csm.css.getConfigQrPay", data: JSON.stringify(data)
    });
  }
  //轮询单子是否成功
  isQrPaySucc(data) {
    return this.http.get("/Inbound/invoke.do", {
      interfaceId: "csm.css.getWeChatOrder", data: JSON.stringify(data)
    });
  }

  //保存微信OpenId
  insertWXOpenId(partyId: string, opendId: any) {
    if (!opendId) {
      return;
    }
    this.http.get("/Inbound/dispatch.do", {
      interfaceId: "csm.login.insertpartywechat",
      data: JSON.stringify({"partyId": partyId, "opendId": opendId})
    })
      .subscribe(msg => {
      }, error => {
      });
  }

  /**
   * 登陆
   * @param user
   * @returns {Observable<any>}
   */
  login(user) {
    return this.http.get("/trust/Inbound/dispatch.do", {
      interfaceId: "csm.login.query",
      data: JSON.stringify({"IDVALUE": user.userId, "PASSWORD": user.password})
    });
  }

  /**
   * 查询用户收获地址
   * @param PARTYID
   * @returns {Observable<any>}
   */
  getmorendizhi(PARTYID) {
    return this.http.get("/Inbound/invoke.do", {
      interfaceId: "csm.yswc.getmorendizhi.query",
      data: JSON.stringify({"PARTYID": PARTYID})
    });
  }

  /**
   *   查询用户默认地址
   * @param PARTYID
   * @returns {Observable<any>}
   */
  userfacility(PARTYID) {
    return this.http.get("/Inbound/invoke.do", {
      interfaceId: "csm.ayjk.userfacility.query",
      data: JSON.stringify({"partyId": PARTYID})
    });
  }


  /**
   * 发送验证码
   * @param mobile
   * @returns {Observable<any>}
   */
  putkey(mobile): Observable<any> {
    return this.http.post("/trust/Inbound/invoke.do", {
      interfaceId: "csm.login.putkey", data: JSON.stringify(
        {
          "interfaceId": "csm.login.getSMS", "PARTYID": env.PARTY_ID,
          'mobile': mobile, "json": {}
        })
    });
  }

  /**
   * 验证 验证码
   * @param mobile
   * @param key
   * @returns {Observable<any>}
   */
  getkey(mobile, key): Observable<any> {
    return Observable.create((observer) => {
      this.http.post("/trust/Inbound/invoke.do", {
        interfaceId: "csm.login.getkey",
        data: JSON.stringify({'mobile': mobile, 'key': key})
      })
        .subscribe(message => {
          observer.next(message);
          observer.complete();
        }, error => {
          observer.error(error);
        });
    });
  }
}
